/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.securitysubsystem;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Francesco
 */
public class PasswordManager 
{

    // Instance Fields
    
    private String hashAlgo ;
    
    // Instance Methods
    
    public PasswordManager () 
    {
        hashAlgo = "MD5" ;
    }
    
    public String hash ( String password ) 
    {
        byte [] byteData ;
        StringBuffer stringBuffer ;
        MessageDigest messageDigest ;
        String res ;
        int i ;
        try 
        {    
            messageDigest = MessageDigest.getInstance ( hashAlgo ) ;
            messageDigest.update ( password.getBytes () ) ;
            byteData = messageDigest.digest();
            stringBuffer = new StringBuffer () ;
            for ( i = 0 ; i < byteData.length ; i ++ )
                stringBuffer.append ( Integer.toString ( ( byteData [i] & 0xff ) + 0x100,16 ).substring(1));
            res = stringBuffer.toString();
        } 
        catch (NoSuchAlgorithmException ex) 
        {
            throw new IllegalArgumentException () ;
        }
        return res ;
    }
    
    public PasswordEvaluationResult evaluate ( String password ) 
    {
        PasswordEvaluationResult res ;
        if ( password != null )
        {
            if ( password.length() < 6 )
                res = PasswordEvaluationResult.LOW ;
            else
            {
                res = PasswordEvaluationResult.HIGH ;
            }
        }
        else
            throw new IllegalArgumentException () ;
        return res ;
    }
    
    // Other Members
    
    /***/
    public enum PasswordEvaluationResult 
    {
    
        /***/
        LOW ,
        
        /***/
        MEDIUM ,
        
        /***/
        HIGH 
    
    }
    
}
